<?php
namespace Weedo\Validate;

/**
 * Weedo EmailAddress Validator class
 *
 * @author http://twitter.com/kaweedo
 */

class EmailAddress extends \Zend_Validate_Abstract
{
    const INVALID_FORMAT     = 'emailAddressInvalidFormat';

    protected $_messageTemplates = array(
         self::INVALID_FORMAT     => "'%value%' is not a valid email address in the basic format local-part@hostname",
    );
 
     /**
     * isValid function.
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if $value is a valid email address
     * according to RFC2822
     *
     * @access public
     * @link   http://php.net/manual/en/function.filter-var.php
     * @param  string $value
     * @return boolean
     */

    public function isValid($value) {

            $this->_setValue($value);
    
        if(filter_var($value, FILTER_VALIDATE_EMAIL) === false) {
            $this->_error(self::INVALID_FORMAT);
            return false;
        } else {
            // We only check the presence of a dot on the domain part
            $components = explode("@", $value);
            $domain = $components[1];
            
            if (strpos($domain, ".") === false) {
                $this->_error(self::INVALID_FORMAT);
                return false;
            }
            
            return true;
        }
    }
}